public class Mouse {
    int maze[][] = { { 2, 2, 2, 2, 2, 2, 2 }, { 2, 0, 0, 0, 0, 0, 2 }, { 2, 0, 2, 0, 2, 0, 2 }, { 2, 0, 0, 2, 0, 2, 2 },
            { 2, 2, 0, 2, 0, 2, 2 }, { 2, 0, 0, 0, 0, 0, 2 }, { 2, 2, 2, 2, 2, 2, 2 } };

    public void mourse() {
        // 迷宫图初始化
        for (int i = 0; i < 7; i++) {
            for (int j = 0; j < 7; j++) {
                if (maze[i][j] == 2) {
                    System.out.print("+");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }

        // 老鼠起点设置，游戏开始
        find(0, 1);
    }

    // 是否越界
    public boolean isIndexOut(int i, int j) {
        return i < 0 || j > 6;
    }

    // 迷宫路径寻找
    public void find(int i, int j) {
        // 找到任意出口，结束
        if (i == 5 && maze[i][j] == 1) {
            System.out.println("老鼠找到了迷宫的出路，出来了！");
        } else {
            // 左
            int times = 0;
            if (!isIndexOut(i, j - 1)) {
                if (maze[i][j - 1] == 0) {
                    System.out.println("第" + times + "步从坐标(" + i + "),(" + j + ")走到坐标(" + i + "),(" + (j - 1) + ")");
                    times++;
//              走过的路线填充为1，避免重复
                    maze[i][j + 1] = 1;
                    find(i, j + 1);
                }
            }
            if (!isIndexOut(i, j + 1)) {
                // 右
                if (maze[i][j + 1] == 0) {
                    System.out.println("第" + times + "步从坐标(" + i + "),(" + j + ")走到坐标(" + i + "),(" + (j + 1) + ")");
                    times++;
//              走过的路线填充为1，避免重复
                    maze[i][j+1] = 1;
                    find(i, j + 1);
                }
            }
            if (!isIndexOut(i-1, j)) {
                //上
                if (maze[i - 1][j] == 0) {
                    System.out.println("第" + times + "步从坐标(" + i + "),(" + j + ")走到坐标(" + (i - 1) + "),(" + j + ")");
                    times++;
//              走过的路线填充为1，避免重复
                    maze[i - 1][j] = 1;
                }}}}}